package com.wn.icloud.common.utils.sms;


import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.wn.icloud.common.utils.AESUtil;

public class SendSmsUtil {

    public final static String ok = "OK";
    public final static String Message = "Message";
    public final static String serect = "c0e9fcff59ecc3b8";


    public final static String QYMCODE = "SMS_189031127";
    public final static String AK = "LTAI4GCBzakM1YuAda9Gco48";//ali
    public final static String SK = "EHXmx9cU6xyXpROjHS4bAzUXpAy9e5";//ali
    public final static String ALI_URL = "dysmsapi.aliyuncs.com";//短信平台单发接口地址
    public final static String APP_NAME = "企业宝";//短信平台单发接口地址

    public static void main(String[] args) {
        String s = sendSms("17603452718", 123456);
        System.out.println(s);
    }
    public static String sendSms(String phone,Integer randomNumber){
        //创建DefaultProfile  regionId默认为ydy。将、accessKey，SerectKey分别替换为客户在短信平台的accessKey，SerectKey
//        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIlpNXIE7N3TBw", "TgM5z1UwnjckHk8BQnDupyYmyfpKZg");
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", AK, SK);
        IAcsClient client = new DefaultAcsClient(profile);
        //创建发送请求实体
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        //短信平台提供get请求类型接口
        //request.setMethod(MethodType.POST);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //短信平台单发接口地址,xxx.xxx.xx为短信平台运营商的 域名 或 IP+端口
        request.setDomain(ALI_URL);
        //发送参数
        //发送目标手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //短信平台审核通过的短信签名
//        request.putQueryParameter("SignName", "石油钻机设备管理");
        request.putQueryParameter("SignName", APP_NAME);
        //短信平台审核通过的短信模板code
        request.putQueryParameter("TemplateCode", QYMCODE);
        //短信模板中的变量参数
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + randomNumber + "\"}");

        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getData();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String encrypted(String phone){
        return AESUtil.encrypt(phone, serect);
    }
}
